<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: Dikkehein
 * Date: 3-2-15
 * Time: 0:49
 */
class Auth extends CI_Controller {

    function __construct() {
        parent::__construct();
        
    }

    public function index() {
        redirect('auth/login');
    }

    public function login() {
        if ($this->session->userdata('logged_in') == TRUE) {
            redirect('auth/authorized');
        } else {
            $this->load->library('form_validation');
            // Set validation rules for view filters
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email|min_length[5]|max_length[125]');
            $this->form_validation->set_rules('wachtwoord', 'Wachtwoord ', 'required|min_length[5]|max_length[30]');
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('auth/loginform');
            } else {
                $email = $this->input->post('email');
                $wachtwoord = $this->input->post('wachtwoord');
                $this->load->model('Gebruiker_model');
                $query = $this->Gebruiker_model->bestaat($email);
                if ($query->num_rows() == 1) {
                    // One matching row found
                    foreach ($query->result() as $row) {
                        // Call Encrypt library
                        $this->load->library('encrypt');
                        // Generate hash from a their password
                        $hash = $this->encrypt->sha1($wachtwoord);
                        // Compare the generated hash with that in the
                        // database
                        if ($hash != $row->wachtwoord) {
                            // Didn't match so send back to login
                            $data['login_fail'] = true;
                            $this->load->view('auth/loginform', $data);
                        } else {
                            if (!$row->stafplicatie) {
                                $data['no_access'] = true;
                            $this->load->view('auth/loginform', $data);
                            } else {
                                $this->load->model('Kampjaar_model');
                                $kampjaarResult = $this->Kampjaar_model->huidig_kampjaar();
                                $data = array(
                                    'id_gebruiker' => $row->id,
                                    'voornaam' => $row->voornaam,
                                    'achternaam' => $row->achternaam,
                                    'email' => $row->email,
                                    'logged_in' => TRUE,
                                    'kampjaar' => $kampjaarResult['jaar']
                                );
                                // Save data to session
                                $this->session->set_userdata($data);
                                redirect('auth/authorized');
                            }
                        }
                    }
                }
            }
        }
    }

    function authorized() {
        if ($this->session->userdata('logged_in') == TRUE) {
            redirect('home');
        } else {
            redirect('auth');
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect('auth');
    }

}